// pages/questions/examination/settype/settype.js
const utils = require('../../../../utils/util.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    // kslx:[
    //   {checked:true,id:'lx_1',value:1,name:'考试资格证'},
    //   {checked:false,id:'lx_2',value:2,name:'教师招聘'}
    // ],
    // ksxd:[
    //   {checked:true,id:'xd_1',value:1,name:'幼儿园'},
    //   {checked:false,id:'xd_2',value:2,name:'小学'},
    //   {checked:false,id:'xd_3',value:3,name:'中学'}
    // ],
    // ksxk:[
    //   {checked:true,id:'xk_0',value:0,name:'全部'},
    //   {checked:false,id:'xk_1',value:1,name:'语文'},
    //   {checked:false,id:'xk_2',value:2,name:'数学'},
    //   {checked:false,id:'xk_3',value:3,name:'英语'},
    //   {checked:false,id:'xk_4',value:4,name:'音乐'},
    //   {checked:false,id:'xk_5',value:5,name:'体育'},
    //   {checked:false,id:'xk_6',value:6,name:'美术'},
    //   {checked:false,id:'xk_7',value:7,name:'其他'}
    // ],
    // ksdq:[
    //   {checked:true,id:'province_1',value:1,name:'江苏'},
    //   {checked:false,id:'province_2',value:2,name:'安徽'},
    //   {checked:false,id:'province_3',value:3,name:'浙江'}
    // ]
    kslx: null,
    ksxd: null,
    ksxk: null,
    ksdq: null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let _this = this;
    utils.requests({
      url: 'questions/examinationType/lists',
      success: function(data) {
        if ( data.data.code == 200 ) {
          _this.setData(data.data.data);
          setType(_this);
        }
      }
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  // 选择考试类型
  checkRadio: function(e){
    let _this = this;
    let id = e.currentTarget.id;
    let type = e.currentTarget.dataset.type;
    let data = _this.data[type];
    for ( let key in data ) {
      if ( key == id ) {
        data[key].checked = true;
      } else {
        data[key].checked = false;
      }
    }
    _this.setData({
      [type]:data
    })
  },

  // 设置用户考试类型
  setting: function(e){
    let data = e.detail.value;
    utils.requests({
      url: 'user/setting/examinationType',
      method: 'post',
      data: data,
      success: function(data){
        if ( data.data.code == 200 ) {
          wx.showToast({
            title: "设置成功",
            duration: 1500
          })
        }
      }
    })
  }

})

// 设置用户考试类型
let setType = (_this) => {
  utils.requests({
    url: 'user/examinationType',
    success: function(data){
      if ( data.data.code == 200 ) {
        let res = data.data.data;
        let kslx = _this.data.kslx;
        for ( let key in kslx ) {
          if ( kslx[key].value == res.examination_type ) {
            kslx[key].checked = true;
          } else {
            kslx[key].checked = false;
          }
        }
        _this.setData({kslx:kslx})
        let ksxd = _this.data.ksxd;
        for ( let key in ksxd ) {
          if ( ksxd[key].value == res.examination_section ) {
            ksxd[key].checked = true;
          } else {
            ksxd[key].checked = false;
          }
        }
        _this.setData({ksxd:ksxd})
        let ksxk = _this.data.ksxk;
        for ( let key in ksxk ) {
          if ( ksxk[key].value == res.examination_discipline ) {
            ksxk[key].checked = true;
          } else {
            ksxk[key].checked = false;
          }
        }
        _this.setData({ksxk:ksxk})
        let ksdq = _this.data.ksdq;
        for ( let key in ksdq ) {
          if ( ksdq[key].value == res.examination_area ) {
            ksdq[key].checked = true;
          } else {
            ksdq[key].checked = false;
          }
        }
        _this.setData({ksdq:ksdq})
      }
    },
    complete: function(data){
      if ( data.data.code != 200 && data.data.code != 404 ) {
        wx.showToast({
          title: data.data.message,
          icon: 'none',
          duration: 1500
        })
      }
    }
  })
}